package com.efast.dmom.mgmt.controller.les.pda;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.efast.cafe.framework.bean.ResultBean;
import com.efast.dmom.mgmt.bean.les.LocDlocWhBean;
import com.efast.dmom.mgmt.entity.les.TwMovementDetails;
import com.efast.dmom.mgmt.entity.les.TwMovementHeader;
import com.efast.dmom.mgmt.service.les.TwMovementDetailsService;
import com.efast.dmom.mgmt.service.les.TwMovementHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 库存移动单（表头） 前端控制器
 * </p>
 *
 * @author ChenLiPeng
 * @since 2018-05-16
 */
@Controller
@RequestMapping("/pdaTwMovementHeader")
@Api(description = "PDA->库内->库存移动单执行-PDA（表头)")
public class PdaTwMovementHeaderController {

	private final TwMovementHeaderService twMovementHeaderService;
	private final TwMovementDetailsService twMovementDetailsService;

	@Autowired
	public PdaTwMovementHeaderController(TwMovementHeaderService twMovementHeaderService,
			TwMovementDetailsService twMovementDetailsService) {
		this.twMovementHeaderService = twMovementHeaderService;
		this.twMovementDetailsService = twMovementDetailsService;
	}

	@ApiOperation(value = "新增")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public @ResponseBody
	ResultBean line(@RequestBody TwMovementHeader twMovementHeader) {
		ResultBean result = new ResultBean(true, "'");
		twMovementHeaderService.save(twMovementHeader);
		result.setMessage("新增成功");
		return result;
	}

	@ApiOperation(value = "通过移动单号查询数据")
	@RequestMapping(value = "/queryBySheetNo", method = RequestMethod.POST)
	public @ResponseBody ResultBean queryBySheetNo(@RequestBody TwMovementHeader twMovementHeader) {
		ResultBean result = new ResultBean(true, "'");
		List<TwMovementDetails> details = twMovementDetailsService.selectList(
				new EntityWrapper<TwMovementDetails>().where("sheet_no={0}", twMovementHeader.getSheetNo()));
		result.setObject(details);
		return result;
	}

	@ApiOperation(value = "【通过仓库查询库区】", notes = "请求参数：{\"wareHouseNo\":\"XXX\"}")
	@RequestMapping(value = "/queryDlocByWh", method = RequestMethod.POST)
	public @ResponseBody ResultBean queryDlocByWh(@RequestBody Map<Object, Object> map) {
		ResultBean result = new ResultBean(true, "'");
		List<LocDlocWhBean> locDlocWhBean = twMovementHeaderService.queryDlocByWh((String) map.get("wareHouseNo"));
		result.setObject(locDlocWhBean);
		return result;
	}

	@ApiOperation(value = "【通过库区查询库位】", notes = "请求参数：{\"dlocNo\":\"XXX\"}")
	@RequestMapping(value = "/queryLocByDloc", method = RequestMethod.POST)
	public @ResponseBody ResultBean queryLocByDloc(@RequestBody Map<Object, Object> map) {
		ResultBean result = new ResultBean(true, "'");
		List<LocDlocWhBean> locDlocWhBean = twMovementHeaderService.queryLocByDloc((String) map.get("dlocNo"));
		result.setObject(locDlocWhBean);
		return result;
	}

	/**
	 * 分配完成后，源分配表和目标分配表插入数据
	 * 
	 * @param twMovementHeader
	 *            头表
	 * @return ResultBean
	 */
	@ApiOperation(value = "分配", notes = "有源库位和目标库位的，则不会分配。没有则会分配")
	@RequestMapping(value = "/distribution", method = RequestMethod.POST)
	public @ResponseBody ResultBean distribution(@RequestBody TwMovementHeader twMovementHeader) {
		ResultBean result = new ResultBean(true, "'");
		twMovementHeaderService.distribution(twMovementHeader);
		result.setMessage("分配成功");
		return result;
	}

	@ApiOperation(value = "执行", notes = "有源库位和目标库位的，则不会分配。没有则会分配")
	@RequestMapping(value = "/execute", method = RequestMethod.POST)
	public @ResponseBody ResultBean execute(@RequestBody TwMovementHeader twMovementHeader) {
		ResultBean result = new ResultBean(true, "'");
		twMovementHeaderService.execute(twMovementHeader);
		result.setMessage("执行成功");
		return result;
	}

	@ApiOperation(value = "关闭", notes = "关闭")
	@RequestMapping(value = "/close", method = RequestMethod.POST)
	public @ResponseBody ResultBean close(@RequestBody TwMovementHeader twMovementHeader) {
		ResultBean result = new ResultBean(true, "'");
		twMovementHeaderService.close(twMovementHeader);
		result.setMessage("关闭成功");
		return result;
	}
}
